﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BowlingGame
{
    public class Game
    {
        private const uint MAX_NUMBER_OF_THROWS_RESULTS = 22;
        private const uint MAX_NUMBER_OF_FRAMES = 10;
        private uint gameScore;
        private uint[] throwsResults;
        private uint throwsCounter;

        public Game()
        {
            this.gameScore = 0;
            this.throwsCounter = 0;
            this.throwsResults = new uint[MAX_NUMBER_OF_THROWS_RESULTS];
        }

        public void Throw(uint numberOfPins)
        {
            this.throwsResults[throwsCounter] = numberOfPins;
            ++throwsCounter;
        }

        public uint Score()
        {
            uint throwsIterator = 0;

            //for (throwsIterator = 0; throwsIterator < MAX_NUMBER_OF_THROWS_RESULTS; throwsIterator++)
            //{
            //    gameScore += throwsResults[throwsIterator];
            //}

            throwsIterator = 0;
            for (uint frameIterator = 0; frameIterator < MAX_NUMBER_OF_FRAMES; frameIterator++)
            {
                if (throwsResults[throwsIterator] == 10)
                {
                    gameScore += throwsResults[throwsIterator] + throwsResults[throwsIterator + 1] + throwsResults[throwsIterator + 2];
                    throwsIterator += 1;
                }
                else if (throwsResults[throwsIterator] + throwsResults[throwsIterator + 1] == 10)
                {
                    gameScore += throwsResults[throwsIterator] + throwsResults[throwsIterator + 1] + throwsResults[throwsIterator + 2];
                    throwsIterator += 2;
                }
                else 
                {
                    gameScore += throwsResults[throwsIterator] + throwsResults[throwsIterator + 1];
                    throwsIterator += 2;
                }
            }

            return gameScore;
        }

        //private bool isSpare(uint frameIndex)
        //{
        //    if (throwsResults[throwsIterator] + throwsResults[throwsIterator + 1] == 10)
        //    {
        //        return true;
        //    }
        //    return false;
        //}

    }
        
}
